#ifndef QABSTRACTDRIVE_H
#define QABSTRACTDRIVE_H

#include "shared_global.h"

#include <QObject>
#include <QWidget>
#include <QMap>
#include <QList>

#include <QIODevice>
#include <QTimer>

class QDriveMission;
class QDriveValueMap;
class QDriveAttribute;
class QXmlStreamReader;

class SHAREDSHARED_EXPORT QAbstractDrive : public QObject
{
    Q_OBJECT
public:
    explicit QAbstractDrive(QObject *parent = 0);
    ~QAbstractDrive();

    void    start();
    void    stop();

    virtual bool     addDriveMission(QDriveMission *mission);
    QDriveValueMap* driveValueMap();

    QList<QDriveAttribute*> attributes();
    QDriveAttribute     *   attribute(const QString &name);

    void    saveConfig();
    void    readConfig();

    void    saveValue();
    void    readValue();
protected:
    virtual void    initAttribute();
    virtual void    initMission();
    virtual void    analyseData(const QByteArray &data);
    virtual void    initConnect();
    virtual void    releaseConnect();
    virtual QByteArray getSendData(QDriveMission *ms);
     void    sendMission(QDriveMission *ms);
    void    readAttribute(QXmlStreamReader *r);
    void    readAttributes(QXmlStreamReader *r);

    void    readValueRow(QXmlStreamReader *r);
    void    readValueColumn(QXmlStreamReader *r,int row);
    QString readCell(QXmlStreamReader *r);

    void    appendAttribute(QDriveAttribute *attr);
    QDriveMission *needTodo();
    void    missionDone(QDriveMission *ms);

    int  CRC_Value(const QByteArray array, int iLen);
protected slots:
    void    read();
    void    stateChanged(int state);
    void    run();
signals:
    void    attributeChanged();
    void    valueChanged();
public slots:
protected:
    QList<QDriveAttribute*> m_attributes;
    QDriveValueMap          *m_valueMap;
    QList<QDriveMission*>   m_missionList;
    QDriveMission           *m_currentMission;
    QIODevice               *m_device;
    QTimer                  *m_timer;
};

#endif // QABSTRACTDRIVE_H
